<?php
// +----------------------------------------------------------------------
// | 演出类产品类型
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/05/26
// +----------------------------------------------------------------------
// | 特别说明：个人文件，请勿修改，如需修改，请采用类的继承或联系本人
// +----------------------------------------------------------------------
namespace abc777;
class Product_H{
	
	protected $db = null;
	
	public function __construct($db){
		$this->db = $db;
	}
	
	
	// 根据日期获取演出所有场次
	public function rounds($venus_id, $date, $storage=0){
		
		// 获取场馆分区
		$area_list = $this->zone_list($venus_id);
		
		// 处理预售
		$presale = (strtotime($date) - strtotime(date('Y-m-d',time()+96400))) / 86400;	
		
		// 获取场次
		$sql = "select id,round_name,bt,et,venus_id,use_date from pft_round where venus_id=$venus_id and use_date='$date' and status=0 and pre_sale>=$presale";
		$stmt = $this->db->prepare($sql);
        $stmt->execute(); $clsResult = array();
		if($storage==0){
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) $clsResult[] = $row;
		}else{// 获取库存座位信息
			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
				// print_r(array($row['id'], $row['venus_id']));
				$seats = $this->round_seat_list($row['id'], $row['venus_id']);
				// echo count($seats);
				// 初始分区所有座位库存为0
				$area_seat = array();
				foreach($area_list as $aid=>$area_name){
					if(!isset($area_seat[$aid])) $area_seat[$aid] = 0;
				}
				foreach($seats as $key=>$row_seat) $area_seat[$row_seat['zone_id']] = $area_seat[$row_seat['zone_id']]+1;
				$row['storage'] = count($seats);
				
				$row['area_storage'] = $area_seat;
				$clsResult[] = $row;
			}
		}
		return $clsResult;
	}
	// 根据场馆ID获取分区
	public function zone_list($venus_id){
		$sql = "select id,zone_name from pft_roundzone where venue_id=$venus_id";
		$stmt = $this->db->prepare($sql);
        $stmt->execute(); $clsResult = array();
		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) $clsResult[$row['id']] = $row['zone_name'];
		return $clsResult;
	}
	
	// 根据场次获取座位信息
	public function round_seat_list($round_id, $venue_id){
		
		// 释放座位 0 关闭 1 预留 2 锁定 3 已售出 4 已退 5 不可售
		$releaseSeat = array();
		
		// 获取已出售和锁定、不可售的座位
		$sql  = "select id,seat_id,round_id,status from pft_roundseat_dyn where round_id=$round_id";
		$stmt = $this->db->prepare($sql);
        $stmt->execute(); $dyncSeat = array();
        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
			if($row['status']==4){
				$releaseSeat[$row['seat_id']] = $row;
				continue;
			}
			$dyncSeat[$row['seat_id']] = $row;
		}
		
		if($round_id==3611){
			print_r($dyncSeat);
			exit;
		}
		
		
		// echo count($dyncSeat),'<br/>',$round_id,"<br/>";
		// echo count($releaseSeat),"<br/>";   
		
		// 134 可售
		
		// 获取场馆座位表
		$sql  = "select id,col_num,row_num,custom_num,zone_id,0 as dync_id,row_id,col_id from pft_roundseat where seat_status=0 and venue_id=$venue_id";
		$stmt = $this->db->prepare($sql);
        $stmt->execute(array($venue_id)); $staticSeat = array();
        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) $staticSeat[$row['id']] = $row;
		// print_r($staticSeat);
		// echo count($staticSeat);
		// 去除已出售及锁定即剩余座位-库存
		$surplus = array_diff_key($staticSeat, $dyncSeat);
		foreach($releaseSeat as $seat_id=>$row) if(isset($surplus[$seat_id])) $surplus[$seat_id]['dync_id'] = $row['id'];

		$col_px = array();
		$row_px = array();
		foreach($surplus as $row){
			$col_px[] = $row['col_id'];
			$row_px[] = $row['row_id'];
		}
		
		usort($surplus,function($a,$b){
			if($a['row_id']==$b['row_id']) return 0;
			return ($a['row_id']>$b['row_id']) ?  1:-1;
		});
		

		$big_arr = array();
		foreach($surplus as $row){
			$big_arr[$row['row_id']][] = $row;
		}
		
		foreach($big_arr as $key=>$row){
			usort($row,function($a,$b){
				if($a['col_id']==$b['col_id']) return 0;
				return ($a['col_id']>$b['col_id']) ?  1:-1;
			});
			$big_arr[$key] = $row;
		}
		
		$sort_result = array();
		foreach($big_arr as $row){
			$sort_result = array_merge($sort_result, $row);
		}
		
		$surplus = $sort_result;
		// if($_SESSION['sid']==1433){
			// print_r($surplus);
			// exit;
		// }
		return $surplus;
	}

	
	
	// 动态新增用户座位 成功返回动态座位表ID
	// $round_id 场次ID  $venue_id   场馆ID
	// $area_id  分区ID  $ticket_num 预定张数
	public function dync_add_seat($round_id, $venue_id, $area_id, $ticket_num=1, $buyid=0){
		
		// print_r(array($round_id, $venue_id, $area_id, $ticket_num=1, $buyid));
		// exit;
		if($buyid==0) $buyid = $_SESSION['sid'];
		$dyn_seat_id = array(); $count = 0;
		$seats = $this->round_seat_list($round_id, $venue_id);

		if(count($seats)==0) return array();// 没有座位
		foreach($seats as $key=>$seat_row){
			if($seat_row['zone_id']!=$area_id) continue;
			if($seat_row['dync_id']>0){// 预定已经释放的座位
				$ctime = time();
				$sql = "update pft_roundseat_dyn set round_id=?,seat_id=?,status=2,opid=?,create_time='$ctime',zone_id=? where id=?";
				$stmt = $this->db->prepare($sql);
				if($stmt->execute(array($round_id, $seat_row['id'], $buyid, $area_id, $seat_row['dync_id']))){
					$dyn_seat_id[] = $seat_row['dync_id'];
					$count++;
				}
			}else{
				$ctime = time();
				$sql = "insert into pft_roundseat_dyn set round_id=?,seat_id=?,status=2,opid=?,create_time='$ctime',zone_id=?";
				$stmt = $this->db->prepare($sql);
				if($stmt->execute(array($round_id, $seat_row['id'], $buyid, $area_id))){
				// if(true){
					$sql = "select id from pft_roundseat_dyn where round_id=? and seat_id=? and status=2 and opid=? limit 1";
					// $sql = "select id from pft_roundseat_dyn where round_id=$round_id and seat_id=202 and status=2 and opid=$buyid limit 1";
					// echo $sql;
					$stmt = $this->db->prepare($sql);
					$stmt->execute(array($round_id, $seat_row['id'], $buyid));
					$dbRow = $stmt->fetch(\PDO::FETCH_ASSOC);
					$dyn_seat_id[] = $dbRow['id'];
					$count++;
				}
			}
			if(($count)==$ticket_num) break;
		}
		if($count<$ticket_num){// 座位不足 释放锁定
			$sql = "update pft_roundseat_dyn set status=4 where status=2 and opid=? and round_id=? and zone_id=? limit $ticket_num";
			$stmt = $this->db->prepare($sql);
			$stmt->execute(array($buyid, $round_id, $area_id));
			return array();
		}
		return $dyn_seat_id;
	}
	
	// 释放座位
	public function release_dync_seat($round_id, $venue_id, $area_id, $ticket_num, $buyid){
		$sql = "update pft_roundseat_dyn set status=4 where round_id=? and status=2 and zone_id=? and opid=? order by id desc limit $ticket_num";
		$stmt = $this->db->prepare($sql);
		return $stmt->execute(array($round_id,$area_id,$buyid));
	}
	
	// 更新座位订单信息
	public function update_seat_order($ordernum, $buyid, $tnum, $round_id, $area_id, $status=3){
		$sql = "update pft_roundseat_dyn set ordernum=?,status=? where status=2 and ordernum=0 and opid=? and round_id=? and zone_id=? limit $tnum";
		$stmt = $this->db->prepare($sql);
		return $stmt->execute(array($ordernum, $status, $buyid, $round_id, $area_id));
	}
	
	public function update_seat_order_after_pay($ordernum, $buyid){
		$sql = "update pft_roundseat_dyn set status=3 where ordernum=? and status=2 and opid=?";
		$stmt = $this->db->prepare($sql);
		return $stmt->execute(array($ordernum, $buyid));
	}
	
	
	// 根据订单释放座位
	public function release_order_seat($ordernum, $ticket_num){
		$sql = "update pft_roundseat_dyn set ordernum='',status=4 where ordernum=? and status=3 order by id desc limit $ticket_num";
		$stmt = $this->db->prepare($sql);
		return $stmt->execute(array($ordernum));
	}
	
	// 调试日志
	public static function write_log($txt, $suffix=''){
		$file = '/var/www/html/new/d/abc/logs/abc777_debug_'.$suffix.'_'.date('Ymd').'.txt';
		$fp = fopen($file,"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}